package com.ccx.coachsite.versionupdate;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.data.SystemConst;
import com.ccx.coachsite.util.SUtil;
import com.ccx.coachsite.util.U;

public class WaitForDownloadDialog extends Activity {
	
	private String url = null;
	private String name = null;

	private TextView nameView = null;
	private ProgressBar bar = null;
	private TextView progressTextView = null;
	private TextView tipView = null;
	private Button stopView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_waitfor_download);
		url = getIntent().getStringExtra("URL");
		name = getIntent().getStringExtra("NAME");
		initTopView();
		initProgressView();
		initBottomView();
		initStopView();
		//U.createHideInputMethod(this);
		//开始下载
		SoftUpdateDownloadThread mSoftUpdateDownloadThread = new SoftUpdateDownloadThread(mHandler, url, name);
		mSoftUpdateDownloadThread.start();
		//添加至下载线程管理器中
		//DownloadThreadManager.getInstance().addThread(url, mSoftUpdateDownloadThread);
	}

	private void initStopView() {
		stopView = (Button) findViewById(R.id.stop_download);
		stopView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//DownloadThreadManager.getInstance().cancelThread(url);
				finish();
			}
		});
	}

	private void initTopView() {
		nameView = (TextView) findViewById(R.id.download_name);
		nameView.setText(name);
	}

	private void initProgressView() {
		bar = (ProgressBar) findViewById(R.id.download_progress_bar);
		bar.setProgress(0);
	}

	private void initBottomView() {
		tipView = (TextView) findViewById(R.id.download_tip);
		tipView.setTextColor(Color.BLUE);

		progressTextView = (TextView) findViewById(R.id.download_progress_text);
		progressTextView.setText("0 / 0");
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Bundle bundle;
			switch(msg.what){
			case SoftUpdateDownloadThread.MSG_CODE_DOWNLOAD_START:
				bundle = msg.getData();
				long size = bundle.getLong("fileSize", 0);
				progressTextView.setText("0 / " + SUtil.parseLongToKbOrMb(size, 2));
				break;
			case SoftUpdateDownloadThread.MSG_CODE_DOWNLOADING:
				bundle = msg.getData();
				long downloadedSize = bundle.getLong("downloadedSize", 0);
				long fileSize = bundle.getLong("fileSize", 0);
				if (fileSize == 0) {
					bar.setProgress(0);
				} else {
					bar.setProgress((int) (100 * downloadedSize / fileSize));
				}
				progressTextView.setText(SUtil.parseLongToKbOrMb(downloadedSize,2) + " / " + SUtil.parseLongToKbOrMb(fileSize, 2));
				break;
			case SoftUpdateDownloadThread.MSG_CODE_DOWNLOAD_SUCCESS:
				//DownloadThreadManager.getInstance().removeThread(url);
				Intent mIntent = new Intent(Intent.ACTION_VIEW);
				Uri uri = Uri.parse("file://" + SystemConst.BASE_DIR + "/" + name + ".apk");
				mIntent.setDataAndType(uri, "application/vnd.android.package-archive");
				startActivity(mIntent);
				finish();
				break;
			case SoftUpdateDownloadThread.MSG_CODE_DOWNLOAD_FAILED:
				Toast.makeText(WaitForDownloadDialog.this, R.string.text_unsuccessfully_downloaded, Toast.LENGTH_SHORT).show();
				//DownloadThreadManager.getInstance().removeThread(url);
				finish();
				break;
			default:
				break;
			}
			
		}
	};
	
}
